package com.ruoyi.project.drug.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.drug.domain.DruPurchase;
import com.ruoyi.project.drug.domain.DruPurchaseDrug;
import com.ruoyi.project.drug.mapper.DruPurchaseDrugMapper;
import com.ruoyi.project.drug.mapper.DruPurchaseMapper;
import com.ruoyi.project.drug.service.DruPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class DruPurchaseServiceImpl implements DruPurchaseService {


    @Autowired
    private DruPurchaseMapper druPurchaseMapper;


    @Autowired
    private DruPurchaseDrugMapper druPurchaseDrugMapper;


    /**
     * 根据条件查询采购订单信息
     *
     * @param druPurchase  传递采购订单查询条件参数
     * @return  返回查询结果集合
     */
    @Override
    public List<DruPurchase> selectAll(DruPurchase druPurchase) {
        return druPurchaseMapper.selectAll(druPurchase);
    }



    /**
     * 根据采购单号查询采购单详情信息
     *
     * @param dpid  采购单号
     * @return  返回携带采购订单信息和采购的药品信息集合
     */
    @Override
    public Map<String, Object> selectParticulars(String dpid) {

        Map<String,Object> map = new HashMap<>();

        if ("undefined".equals(dpid)){

            DruPurchase druPurchase = new DruPurchase();

            druPurchase.setDpid("CG1785" + new Date().getTime());
            druPurchase.setApplyUser(SecurityUtils.getUsername());
            druPurchase.setDpTotalAmount(0D);
            druPurchase.setCreateTime(new Date());

            /*int i = druPurchaseMapper.addPurchase(druPurchase);

            if (i == 1){
                map.put("druPurchase",druPurchase);
                map.put("drugList",null);
            }*/
            map.put("druPurchase",druPurchase);
            List<DruPurchaseDrug> drugList = new ArrayList<>();
            map.put("drugList",drugList);

        }else {
            // 获得采购订单信息
            DruPurchase druPurchase = druPurchaseMapper.selectParticulars(dpid);

            // 获得采购的药品信息
            List<DruPurchaseDrug> drugList = druPurchaseDrugMapper.selectAll(dpid);

            map.put("druPurchase",druPurchase);
            map.put("drugList",drugList);
        }

        return map;
    }



    /**
     * 修改采购单信息
     *
     * @param druPurchase  采购单数据
     * @return 成功返回1
     */
    @Override
    public int updatepurchase(DruPurchase druPurchase) {

        int flag = 0;

        int updatepurchase = druPurchaseMapper.updatepurchase(druPurchase);

        flag = updatepurchase;
        if (updatepurchase != 1){
            int i = druPurchaseMapper.addPurchase(druPurchase);

            if (i == 1){
                flag = 1;
            }
        }

        return flag;
    }



    /**
     * 提交审核
     *
     * @param dpids  订单号
     * @return  成功返回1
     */
    @Override
    public int submitAudit(String[] dpids, String state) {

        if ("0".equals(state)){

            for (String dpid:dpids) {
                DruPurchase druPurchase = new DruPurchase();
                druPurchase.setDpid(dpid);
                druPurchase.setOperationUser(SecurityUtils.getUsername());
                druPurchase.setEnterDate(new Date());

                int i = 0;
                try {
                    i = druPurchaseMapper.updateOperationUser(druPurchase);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return druPurchaseMapper.submitAudit(dpids,state);
    }


    /**
     * 根据订单号查询信息
     *
     * @return
     */
    @Override
    public int deletePurchase(String[] dpids) {
        return druPurchaseMapper.deletePurchase(dpids);
    }
}
